package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtDraw;
import com.greyu.ysj.entity.CbtDrawExample;
import com.greyu.ysj.entity.CbtExchange;
import com.greyu.ysj.entity.CbtExchangeExample;
import com.greyu.ysj.mapper.CbtExchangeMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtExchangeService;

@Service
@Transactional
public class CbtExchangeServiceImpl implements CbtExchangeService{

	@Autowired
	private CbtExchangeMapper cbtExchangeMapper;
	
	@Override
	public ResultModel save(CbtExchange cbtExchange) {
		try {	
			if ( cbtExchange.getAccount()==null
					||cbtExchange.getAmount()==null
					||cbtExchange.getDrawDate()==null
					||cbtExchange.getUserId()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtExchangeMapper.insert(cbtExchange);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.DRAW_INSERT_ERROR);
		}
	        return ResultModel.ok(cbtExchange);
	}

	@Override
	public List<CbtExchange> selectExchangeRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtExchangeExample cbtExchangeExample=new CbtExchangeExample();
		CbtExchangeExample.Criteria criteria=cbtExchangeExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		cbtExchangeExample.setOrderByClause("`Id` DESC");
		List<CbtExchange> list=cbtExchangeMapper.selectByExample(cbtExchangeExample);
		
		if(list==null || list.size()<1){
			
			return null;
		}
		return list;
	}

	@Override
	public int totalPages(Integer userId, Integer rows) {
		CbtExchangeExample cbtExchangeExample=new CbtExchangeExample();
		CbtExchangeExample.Criteria criteria=cbtExchangeExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		long total=cbtExchangeMapper.countByExample(cbtExchangeExample);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}

	@Override
	public int selectMaxExchangeId() {
		int drawId=this.cbtExchangeMapper.selectMaxExchangeId();
		return drawId;
	}

	@Override
	public CbtExchange selectCbtExchangeById(Integer id) {
		return this.cbtExchangeMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResultModel updateCbtExchange(Integer id, CbtExchange cbtExchange) {
		CbtExchange newCbtExchange=this.cbtExchangeMapper.selectByPrimaryKey(id);
		if(newCbtExchange == null) {
			 return ResultModel.error(ResultStatus.DRAW_NOT_FOUND);
		}
		if(cbtExchange.getStatus() != null){
			newCbtExchange.setStatus(cbtExchange.getStatus());
		}
		 this.cbtExchangeMapper.updateByPrimaryKey(newCbtExchange);
		return ResultModel.ok(newCbtExchange);
	}

	@Override
	public boolean isHashExist(String hash) {
		CbtExchangeExample cbtExchangeExample = new CbtExchangeExample();
		CbtExchangeExample.Criteria criteria = cbtExchangeExample.createCriteria();
		criteria.andDrawHashEqualTo(hash);
        List<CbtExchange> list = this.cbtExchangeMapper.selectByExample(cbtExchangeExample);
        if(list==null||list.size()<1) {
        	return false;
        }else {
        	
        	return true;
        }
	}

	@Override
	public List<CbtExchange> selectAll(CbtExchange cbtExchange) {
		CbtExchangeExample cbtExchangeExample = new CbtExchangeExample();
		CbtExchangeExample.Criteria criteria = cbtExchangeExample.createCriteria();
		criteria.andStatusEqualTo(cbtExchange.getStatus());
        List<CbtExchange> list = this.cbtExchangeMapper.selectByExample(cbtExchangeExample);
        return list;
	}

	@Override
	public double selectTotalExchangeByuserId(int userId) {
		double totalDraw=this.cbtExchangeMapper.selectTotalExchangeByuserId(userId);
		return totalDraw;
	}

}
